package com.zmn.plat.dubbo.impl.push;

import java.util.List;

import javax.annotation.Resource;

import com.zmn.manager.common.database.annotation.ReadOnlyConnection;
import org.apache.dubbo.config.annotation.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.dto.ResponseDTO;
import com.zmn.common.dto.push.inner.InnerDTO;
import com.zmn.common.utils.response.ResponseUtil;
import com.zmn.plat.dubbo.interfaces.push.InnerRemoteService;
import com.zmn.plat.dubbo.utils.DubboConsts;
import com.zmn.plat.model.entity.inner.InnerQuery;
import com.zmn.plat.services.interfaces.push.PushInfoService;

/**
 * @author duanzuocai
 * @version v1.0
 * @since 2019/1/3 10:29
 */
@Service(loadbalance = "roundrobin", timeout = 5000, retries = -1, version = DubboConsts.INTERFACE_VERSION, validation = "true")
public class InnerRemoteServiceImpl implements InnerRemoteService {
    private Logger logger = LoggerFactory.getLogger(InnerRemoteServiceImpl.class);

    @Resource
    private PushInfoService pushInfoService;

    @Override
    @ReadOnlyConnection
    public ResponseDTO<InnerDTO> getOneInnerMsg(Integer staffId) {
        if (staffId == null || staffId < 1) {
            return ResponseUtil.responseSuccess(null, GlobalConsts.SUCCESS_MSG);
        }

        InnerDTO dto = pushInfoService.findOneInnerMsg(staffId);

        if (dto != null) {
            logger.info("[zmn]读取站内信：dto:{}", dto);
        }

        return ResponseUtil.responseSuccess(dto, GlobalConsts.SUCCESS_MSG);
    }

    @Override
    @ReadOnlyConnection
    public ResponseDTO<InnerDTO> listPageInnerMsg(Integer staffId, Integer status, Integer pageIndex, Integer pageSize) {
        InnerQuery query = new InnerQuery(staffId, status, null);
        query.setPageIndex(pageIndex);
        query.setPageSize(pageSize);

        List<InnerDTO> list = pushInfoService.listPageByQuery(query);

        return ResponseUtil.responseSuccess(list, GlobalConsts.SUCCESS_MSG);
    }

    @Override
    @ReadOnlyConnection
    public boolean readOneInnerMsg(Integer innerId) {
        int rel = pushInfoService.readOneInnerMsg(innerId);

        logger.info("[zmn]更新站内信：innerId:{}, 结果:{}", innerId, rel);

        return rel == 1 ? true : false;
    }

    @Override
    @ReadOnlyConnection
    public ResponseDTO<InnerDTO> getReadOneInnerMsg(Integer staffId) {
        if (staffId == null || staffId < 1) {
            return ResponseUtil.responseSuccess(null, GlobalConsts.SUCCESS_MSG);
        }

        InnerDTO dto = pushInfoService.findReadOneInnerMsg(staffId);

        if (dto != null) {
            logger.info("[zmn]读取并更新站内信：staffId:, dto:{}", staffId, dto);
        }

        return ResponseUtil.responseSuccess(dto, GlobalConsts.SUCCESS_MSG);
    }
}
